package com.geese.module.ceo.controller;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Building;
import com.geese.common.entity.Courier;
import com.geese.common.util.BeanUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.exception.BusinessException;
import com.geese.module.ceo.dto.BuildingDto;
import com.geese.module.ceo.service.BuildingService;
import com.geese.module.ceo.service.CourierService;
import com.geese.module.ceo.vo.BuildingQuery;
import com.geese.module.ceo.vo.BuildingVO;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/ceo")
@Scope("prototype")
public class BuildingController extends BaseController {

    @Autowired 
    private UserService userService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private CourierService courierService;


    /**
     * 新增楼栋
     */
    @PostMapping("/building/save")
    @ResponseBody
    public String buildingSave(@RequestBody BuildingVO buildingVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(buildingVO.getCampusId())) {
    		throw new BusinessException(120039);
    	}
    	if(StringUtils.isEmpty(buildingVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	Building building = BeanUtil.map(buildingVO, Building.class);

    	userService.checkUser(token);
    	buildingService.save(building);
        return response();
    }

    /**
     * 批量删除楼栋
     */
    @PostMapping("/building/batchRemove")
    @ResponseBody
    public String buildingBatchRemove(@RequestParam String token, @RequestParam String ids){
    	List<String> tempIds = Splitter.on(",").trimResults().splitToList(ids);
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(120016);
    	}
    	List<Integer> buildingIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	userService.checkUser(token);
    	buildingService.batchRemove(buildingIds);
        return response();
    }

    /**
     * 修改楼栋
     */
    @PostMapping("/building/update")
    @ResponseBody
    public String buildingUpdate(@RequestBody BuildingVO buildingVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(buildingVO.getId())) {
    		throw new BusinessException(120038);
    	}
    	if(StringUtils.isEmpty(buildingVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	if(!StringUtils.isNumeric(buildingVO.getCampusId())) {
    		throw new BusinessException(120039);
    	}
    	
    	userService.checkUser(token);
    	Building building = BeanUtil.map(buildingVO, Building.class);
    	buildingService.update(building);
        return response();
    }
    
    /**
     * 楼栋列表
     */
    @GetMapping("/building/listInPage")
    @ResponseBody
    public String schoolList(BuildingQuery query) {
    	if(StringUtils.isNotEmpty(query.getCampusId()) 
    			&& !StringUtils.isNumeric(query.getCampusId())) {
    		throw new BusinessException(120039);
    	}
    	
    	userService.checkUser(query.getToken());
    	SecurityUser user = userService.getLoginUser(query.getToken());
    	Courier courier = courierService.findByUserId(user.getUserId());
    	
    	Page page = getPageInfo(query);
    	Paginator<BuildingDto> pageList = buildingService.listAllByQuery(courier.getSchoolId(), query, page);
    	Map<String, Object> result = Maps.newHashMap();
    	result.put(Constants.PAGE_INFO, pageList.getPageResult());
    	result.put("buildingList", pageList.getResults());
        return response(result);
    }
}
